package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SaleListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-30 11:51
 */
@Service
public class SaleListServiceImpl implements SaleListService {

    @Autowired
    private SaleListGoodsDao saleListGoodsDao;

    @Autowired
    private GoodsDao goodsDao;

    //销售单保存
    @Override
    public ServiceVO saleListGoodsListSave(SaleList saleList, List<SaleListGoods> saleListGoodsList) {
        //保存新增销售单
        saleListGoodsDao.saveSaleList(saleList);
        Integer saleListId = saleList.getSaleListId();

        //保存商品信息
        for (SaleListGoods saleListGoods : saleListGoodsList) {
            //设置销售单id
            saleListGoods.setSaleListId(saleListId);
            //调用保存方法
            //保存新增销售单商品
            saleListGoodsDao.saveSaleListGoods(saleListGoods);
            //查询商品库存  状态
            Goods saGoods = goodsDao.findByGoodsId(saleListGoods.getGoodsId());
            saGoods.setSellingPrice(saleListGoods.getPrice());

            //设置新总库存
            saGoods.setInventoryQuantity(saGoods.getInventoryQuantity() + saleListGoods.getGoodsNum());
            saGoods.setState(2);
            //修改
            goodsDao.updateGoods(saGoods);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    //条件查询
    @Override
    public Map<String, Object> getSaleSearchList(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        //查询结果返回列表
        List<SaleList> saleSearchList = saleListGoodsDao.getSaleSearchList(saleNumber, customerId, state, sTime, eTime);
        //将查询结果放入map
        map.put("rows", saleSearchList);
        //返回map数据
        return map;
    }


    //查询销售单商品信息
    @Override
    public Map<String, Object> getSaleGoodsList(Integer saleListId) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        //查询结果返回对象
        List<SaleList> saleGoodsList = saleListGoodsDao.getSaleGoodsList(saleListId);
        map.put("rows",saleGoodsList);
        return map;
    }


    //销售单删除
    @Override
    public ServiceVO deleteSaleListGoods(Integer saleListId) {
        saleListGoodsDao.deleteSaleListGoods(saleListId);
        saleListGoodsDao.deleteSaleList(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    //支付结算（修改进货单付款状态）
    @Override
    public ServiceVO updateSaleListGoods(Integer saleListId) {
        saleListGoodsDao.updateSaleList(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
